package com.aube.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aube.BaseFragmentActivity;
import com.aube.R;
import com.aube.views.RealtimeBlurView;
import com.huyn.baseframework.share.QQUtil;
import com.huyn.baseframework.share.WxUtil;
import com.ryg.dynamicload.DLProxyFragmentActivity;

/**
 * Created by huyaonan on 16/6/12.
 */
public class ForceLoginHelper implements View.OnClickListener {
    private final String TAG = "ForceLoginHelper";

    private Activity mActivity;
    private View closeBtn, mLayout, mLoginWeibo, mLoginWechat, mLoginQQ;

    private RealtimeBlurView mBlur;
    private View mRoot;
    private static final int ANIM_DURATION = 400;

    public ForceLoginHelper(Activity activity) {
        this.mActivity = activity;
    }

    public void showLoginLayout() {
        if(mActivity == null)
            return;

        if(mRoot == null) {
            mRoot = mActivity instanceof DLProxyFragmentActivity ? inflate(LayoutInflater.from(mActivity), ((DLProxyFragmentActivity) mActivity).getSuperResources(), R.layout.layout_login)
                                                                    : LayoutInflater.from(mActivity).inflate(R.layout.layout_login, null);
            mActivity.addContentView(mRoot, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            findViews(mRoot);
        } else {
            animIn();
        }
    }

    public View inflate(LayoutInflater inflater, Resources res, int id) {
        final XmlResourceParser parser = res.getLayout(id);
        try {
            return inflater.inflate(parser, null, false);
        } finally {
            parser.close();
        }
    }

    private void findViews(View view) {
        view.setOnClickListener(this);
        LinearLayout contentLayout = (LinearLayout) view.findViewById(R.id.dialog_flow_content);
        contentLayout.setOnClickListener(this);

        mLayout = view.findViewById(R.id.dialog_layout);

        mLoginWeibo = view.findViewById(R.id.login_weibo);
        mLoginWeibo.setOnClickListener(this);

        mLoginWechat = view.findViewById(R.id.login_wechat);
        mLoginWechat.setOnClickListener(this);

        mLoginQQ = view.findViewById(R.id.login_qq);
        mLoginQQ.setOnClickListener(this);

        closeBtn = view.findViewById(R.id.dialog_flow_close);
        closeBtn.setOnClickListener(this);

        mBlur = (RealtimeBlurView) view.findViewById(R.id.dialog_blur);
        mBlur.setOverlayColor(mActivity.getResources().getColor(R.color.blur_color));

        animIn();
    }

    private void animIn() {
        mRoot.setAlpha(0f);
        mRoot.setVisibility(View.VISIBLE);

        ObjectAnimator anim = ObjectAnimator.ofFloat(mRoot, "alpha", 0f, 1f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mBlur.invalidate();
            }
        });
        anim.setDuration(ANIM_DURATION);

        ValueAnimator valueAnim = ValueAnimator.ofFloat(0f, 1.2f, 1f);
        valueAnim.setDuration(ANIM_DURATION);
        valueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mLayout.setScaleX(value);
                mLayout.setScaleY(value);
            }
        });

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(anim, valueAnim);
        mSet.start();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == mLoginWechat.getId())
            WxUtil.wxLoginRequest(mActivity);
        else if(v.getId() == mLoginWeibo.getId())
            ((BaseFragmentActivity) mActivity).weiboLogin();
        else if(v.getId() == mLoginQQ.getId()) {
            QQUtil.getInstance(mActivity).login(mActivity);
        }
        dismiss();
    }

    private void dismiss() {
        ObjectAnimator anim = ObjectAnimator.ofFloat(mRoot, "alpha", 1f, 0f);
        anim.setDuration(ANIM_DURATION);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mBlur.invalidate();
            }
        });
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mRoot.setVisibility(View.GONE);
            }
        });

        ValueAnimator valueAnim = ValueAnimator.ofFloat(1f, 1.2f, 0f);
        valueAnim.setDuration(ANIM_DURATION);
        valueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mLayout.setScaleX(value);
                mLayout.setScaleY(value);
            }
        });

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(anim, valueAnim);
        mSet.start();
    }

    public boolean dismissLayout() {
        if(mRoot == null)
            return false;
        if(mRoot.getVisibility() == View.VISIBLE) {
            dismiss();
            return true;
        }
        return false;
    }
}
